Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App: create default config file in launch script #3630

Merged
merged 1 commit into from
Jun 16, 2017
Merged

Conversation

Morfent
Copy link
Contributor

@Morfent Morfent commented Jun 13, 2017

Brought up in #3536 (comment). I don't think it'd be feasible to install the dependencies and import newly recreated config files from app.js because of how much of a hassle that caused with the launch script trying to accomplish the same thing.

Copy link
Contributor

@Slayer95 Slayer95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.exit() is bad practice

app.js Outdated
@@ -49,7 +49,8 @@ const path = require('path');
try {
require.resolve('sockjs');
} catch (e) {
throw new Error("Dependencies unmet; run npm install");
console.error('Dependencies are unmet; run npm install before launching Pokemon Showdown again.');
process.exit(1);
Copy link
Contributor

@Slayer95 Slayer95 Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't dependent-friendly. If another project tries to load pokemon-showdown/app but it fails, it should result in an exception, not in the whole application exiting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what the improvement is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a throw, you can wrap the module load with try-catch.
process.exit() goes through the native layer and can't be prevented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm wondering why anyone would want to use process.exit() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xfix would be able to explain better, since the problems with simply throwing an error instead of exiting don't happen on my system. Maybe it's dependent on OS or the version of Node running

app.js Outdated
@@ -62,14 +63,16 @@ try {
if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen

// Copy it over synchronously from config-example.js since it's needed before we can start the server
console.log("config.js doesn't exist - creating one with default settings...");
console.error("config.js doesn't exist. Creating one with default settings...");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also fine with just not creating one at all, if it's done in launcher.

@Morfent
Copy link
Contributor Author

Morfent commented Jun 16, 2017

Changed this back to throwing errors instead of making the process exit, moved creating the default config file to the launch script

@Morfent Morfent changed the title App: exit safely if dependencies or config.js aren't set up on launch App: create default config file in launch script Jun 16, 2017
@Zarel
Copy link
Member

Zarel commented Jun 16, 2017

Still waiting for @xfix

app.js Outdated
// Check for dependencies
try {
require.resolve('sockjs');
} catch (e) {
throw new Error("Dependencies unmet; run npm install");
throw new Error('Dependencies are unmet; run node pokemon-showdown or npm install before launching Pokemon Showdown again.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just recommend node pokemon-showdown, it's needed to set up config.js anyway.

pokemon-showdown Outdated

// Make sure we're Node 6+

try {
eval('{ let [a] = [1]; }');
} catch (e) {
console.log("We require Node.js v6 or later; you're using " + process.version);
process.exit();
process.exit(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should possibly be an error code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would using 1 (SIGHUP) be appropriate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the most appropriate exit code is EX_UNAVAILABLE (69)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does using the wrong Node version have to do with receiving a signal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...didn't notice that. I'll take a look at how Node deals with exit codes internally, since I'm not positive exiting with errno EX_UNAVAILABLE will do what we're intending to do, judging from the other exit codes below 128

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it uses C error codes judging from the values for process.exitCode used in its unit tests

@Morfent
Copy link
Contributor Author

Morfent commented Jun 16, 2017

Changed the errno to 128 + 69

@Zarel
Copy link
Member

Zarel commented Jun 16, 2017

Why 128?

@Morfent
Copy link
Contributor Author

Morfent commented Jun 16, 2017

@Zarel
Copy link
Member

Zarel commented Jun 16, 2017

Maybe just use code 1 and call it a day.

@Morfent
Copy link
Contributor Author

Morfent commented Jun 16, 2017

Changed the errno to 1

app.js Outdated
if (Config.watchconfig) {
let configPath = require.resolve('./config/config');
fs.watchFile(configPath, (curr, prev) => {
require('fs').watchFile(configPath, (curr, prev) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is the only place fs is required at all in app.js anymore

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convention is to have requires at the top. Making an exception for "only one require" just makes the code inconsistent, and you're going to have to change this if you add more uses anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the main thing is consistency. It's not consistent to have require('fs').watchFile in some parts of the codebase and fs.watchFile in other parts of the codebase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who told you that? I'm personally mostly against inline requires.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kota at one point

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn it, kota.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I did say literally on the same comment thread that I disagreed with kota.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me put it this way, I'm fine with using require() inline if it's the only place the module is required in the entire codebase, and it's an optional module. Otherwise, put it at the top.

@Morfent
Copy link
Contributor Author

Morfent commented Jun 16, 2017

Added back the fs require to the top of the module

@Zarel Zarel dismissed Slayer95’s stale review June 16, 2017 17:10

implemented

@Zarel Zarel merged commit c61b270 into smogon:master Jun 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants